﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SN.UserControl
{
    public partial class CellControl : System.Windows.Forms.UserControl
    {
        public CellControl()
        {
            InitializeComponent();
        }
        int startCol = 0, startRow = 0, endCol = 0, endRow = 0;
        int CurrentRow = 0;

        const int Left = 1;
        const int Right = 2;
        const int CHorizontally = 4;
        const int Top = 8;
        const int Bottom = 16;
        const int CVertically = 32;


        private void SetupFontComboBox()
        {
            AutoCompleteStringCollection ac = new AutoCompleteStringCollection();
            foreach (FontFamily fam in FontFamily.Families)
            {
                fontComboBox.Items.Add(fam.Name);
                ac.Add(fam.Name);
            }
            //leava
            fontComboBox.TextChanged += new EventHandler(fontComboBox_TextChanged);
            fontComboBox.AutoCompleteMode = AutoCompleteMode.Suggest;
            fontComboBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
            fontComboBox.AutoCompleteCustomSource = ac;
        }

        private void SetuFontSize()
        {
            for (int i = 5; i < 15; i++)
            {
                fontSizeComboBox.Items.Add(i.ToString());
            }
            for (int i = 12; i < 31; i = i + 2)
            {
                fontSizeComboBox.Items.Add(i.ToString());
            }
            fontSizeComboBox.Items.Add("36");
            fontSizeComboBox.Items.Add("42");
            fontSizeComboBox.Items.Add("48");
            fontSizeComboBox.Items.Add("72");
            fontSizeComboBox.Items.Add("100");
            fontSizeComboBox.Items.Add("150");

            AutoCompleteStringCollection ac = new AutoCompleteStringCollection();
            fontSizeComboBox.TextChanged += new EventHandler(fontSizeComboBox_TextChanged);
            fontSizeComboBox.AutoCompleteMode = AutoCompleteMode.Suggest;
            fontSizeComboBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
            fontSizeComboBox.AutoCompleteCustomSource = ac;
        }

        //事件
        private void fontComboBox_TextChanged(object sender, EventArgs e)
        {
            axCell.GetSelectRange(ref startCol, ref startRow, ref endCol, ref endRow);
            //FontFamily ff;
            int iFontName;
            try
            {
                //ff = new FontFamily(fontComboBox.Text);
                if (string.IsNullOrWhiteSpace(fontComboBox.Text.Trim())) return;
                iFontName = axCell.FindFontIndex(fontComboBox.Text.Trim(), 1);
                for (int i = startCol; i <= endCol; i++)
                {
                    for (int j = startRow; j <= endRow; j++)
                    {
                        axCell.SetCellFont(i, j, axCell.GetCurSheet(), iFontName);
                    }
                }
            }
            catch (Exception)
            {
                // updatingFontName = true;
                //fontComboBox.Text = FontName.GetName(0);
                //updatingFontName = false;
                return;
            }
            //  FontName = ff;
        }
        //字体大小
        private void fontSizeComboBox_TextChanged(object sender, EventArgs e)
        {
            axCell.GetSelectRange(ref startCol, ref startRow, ref endCol, ref endRow);
            int iFontSize;
            try
            {
                if (string.IsNullOrWhiteSpace(fontSizeComboBox.Text.Trim())) return;

                iFontSize = Convert.ToInt16(fontSizeComboBox.Text);
                for (int i = startCol; i <= endCol; i++)
                {
                    for (int j = startRow; j <= endRow; j++)
                    {
                        axCell.SetCellFontSize(i, j, axCell.GetCurSheet(), iFontSize);
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }

        private void SetCellSize()
        {
            //表尺寸
            frmSetSize frm = new frmSetSize();
            frm.rows = axCell.GetRows(axCell.GetCurSheet());
            frm.cols = axCell.GetCols(axCell.GetCurSheet());
            if (frm.ShowDialog() == DialogResult.OK)
            {
                axCell.SetCols(frm.cols + 1, axCell.GetCurSheet());
                axCell.SetRows(frm.rows + 1, axCell.GetCurSheet());
            }
            return;
        }
        //返回唯一临时文件名并在磁盘上通过该名
        public string GetTempFileName()
        {
            //return System.IO.Path.GetTempPath() + "\\" + System.IO.Path.GetTempFileName() + ".cll";
            string filePath = System.IO.Path.GetTempFileName() + ".cll";
            return filePath;
        }

        //公开的方法
        public int ImportExcelDlg()
        {
            return axCell.ImportExcelDlg();
        }
        public int ExportExcelDlg()
        {
            return axCell.ExportExcelDlg();
        }

        private void Bold() //粗体
        {
            //Checked
            axCell.GetSelectRange(ref startCol, ref startRow, ref endCol, ref endRow);
            for (int i = startCol; i <= endCol; i++)
            {
                for (int j = startRow; j <= endRow; j++)
                {
                    int FontStyle = axCell.GetCellFontStyle(i, j, axCell.GetCurSheet());
                    if (FontStyle == 2)
                    {
                        axCell.SetCellFontStyle(i, j, axCell.GetCurSheet(), axCell.GetCellFontStyle(i, j, axCell.GetCurSheet()) - 2);
                    }
                    else
                    {
                        axCell.SetCellFontStyle(i, j, axCell.GetCurSheet(), axCell.GetCellFontStyle(i, j, axCell.GetCurSheet()) + 2);
                    }
                }
            }

        }

        private void Italic() //斜体
        {
            //Checked
            axCell.GetSelectRange(ref startCol, ref startRow, ref endCol, ref endRow);
            for (int i = startCol; i <= endCol; i++)
            {
                for (int j = startRow; j <= endRow; j++)
                {
                    int FontStyle = axCell.GetCellFontStyle(i, j, axCell.GetCurSheet());
                    if (FontStyle == 4)
                    {
                        axCell.SetCellFontStyle(i, j, axCell.GetCurSheet(), axCell.GetCellFontStyle(i, j, axCell.GetCurSheet()) - 4);
                    }
                    else
                    {
                        axCell.SetCellFontStyle(i, j, axCell.GetCurSheet(), axCell.GetCellFontStyle(i, j, axCell.GetCurSheet()) + 4);
                    }
                }
            }
        }

        private void Underline()//下滑线
        {
            //Checked
            axCell.GetSelectRange(ref startCol, ref startRow, ref endCol, ref endRow);
            for (int i = startCol; i <= endCol; i++)
            {
                for (int j = startRow; j <= endRow; j++)
                {
                    int FontStyle = axCell.GetCellFontStyle(i, j, axCell.GetCurSheet());
                    if (FontStyle == 8)
                    {
                        axCell.SetCellFontStyle(i, j, axCell.GetCurSheet(), axCell.GetCellFontStyle(i, j, axCell.GetCurSheet()) - 8);
                    }
                    else
                    {
                        axCell.SetCellFontStyle(i, j, axCell.GetCurSheet(), axCell.GetCellFontStyle(i, j, axCell.GetCurSheet()) + 8);
                    }
                }
            }
        }

        private void JustifyLeft() //左对齐
        {
            axCell.GetSelectRange(ref startCol, ref startRow, ref endCol, ref endRow);
            for (int i = startCol; i <= endCol; i++)
            {
                for (int j = startRow; j <= endRow; j++)
                {
                    int FontAlign = axCell.GetCellAlign(i, j, axCell.GetCurSheet());

                    if ((FontAlign & Left) != Left) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) + Left);
                    if ((FontAlign & CHorizontally) == CHorizontally) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) - CHorizontally);
                    if ((FontAlign & Right) == Right) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) - Right);

                }
            }
        }

        private void JustifyCenter() //居中对齐
        {
            axCell.GetSelectRange(ref startCol, ref startRow, ref endCol, ref endRow);
            for (int i = startCol; i <= endCol; i++)
            {
                for (int j = startRow; j <= endRow; j++)
                {
                    int FontAlign = axCell.GetCellAlign(i, j, axCell.GetCurSheet());

                    if ((FontAlign & CHorizontally) != CHorizontally) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) + CHorizontally);
                    if ((FontAlign & Left) == Left) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) - Left);
                    if ((FontAlign & Right) == Right) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) - Right);

                }
            }
        }

        private void JustifyRight() //右对齐
        {
            axCell.GetSelectRange(ref startCol, ref startRow, ref endCol, ref endRow);
            for (int i = startCol; i <= endCol; i++)
            {
                for (int j = startRow; j <= endRow; j++)
                {
                    int FontAlign = axCell.GetCellAlign(i, j, axCell.GetCurSheet());

                    if ((FontAlign & Right) != Right) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) + Right);
                    if ((FontAlign & Left) == Left) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) - Left);
                    if ((FontAlign & CHorizontally) == CHorizontally) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) - CHorizontally);

                }
            }
        }

        private void JustifyFull() //垂直居中对齐与水平居中
        {
            axCell.GetSelectRange(ref startCol, ref startRow, ref endCol, ref endRow);
            for (int i = startCol; i <= endCol; i++)
            {
                for (int j = startRow; j <= endRow; j++)
                {
                    int FontAlign = axCell.GetCellAlign(i, j, axCell.GetCurSheet());
                    //垂直居中
                    if ((FontAlign & CVertically) != CVertically) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) + CVertically);
                    if ((FontAlign & Top) == Top) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) - Top);
                    if ((FontAlign & CVertically) == Bottom) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) - Bottom);
                    // 水平居中
                    if ((FontAlign & CHorizontally) != CHorizontally) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) + CHorizontally);
                    if ((FontAlign & Left) == Left) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) - Left);
                    if ((FontAlign & Right) == Right) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) - Right);

                }
            }
        }

        private void VerticalTop()
        {
            //垂直上对齐
            axCell.GetSelectRange(ref startCol, ref startRow, ref endCol, ref endRow);
            for (int i = startCol; i <= endCol; i++)
            {
                for (int j = startRow; j <= endRow; j++)
                {
                    int FontAlign = axCell.GetCellAlign(i, j, axCell.GetCurSheet());

                    if ((FontAlign & Top) != Top) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) + Top);
                    if ((FontAlign & Bottom) == Bottom) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) - Bottom);
                    if ((FontAlign & CVertically) == CVertically) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) - CVertically);

                }
            }
        }

        private void VerticalBottom()
        {
            axCell.GetSelectRange(ref startCol, ref startRow, ref endCol, ref endRow);
            for (int i = startCol; i <= endCol; i++)
            {
                for (int j = startRow; j <= endRow; j++)
                {
                    int FontAlign = axCell.GetCellAlign(i, j, axCell.GetCurSheet());

                    if ((FontAlign & Bottom) != Bottom) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) + Bottom);
                    if ((FontAlign & Top) == Top) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) - Top);
                    if ((FontAlign & CVertically) == CVertically) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) - CVertically);

                }
            }
        }

        private void VerticalCenter()
        {
            axCell.GetSelectRange(ref startCol, ref startRow, ref endCol, ref endRow);
            for (int i = startCol; i <= endCol; i++)
            {
                for (int j = startRow; j <= endRow; j++)
                {
                    int FontAlign = axCell.GetCellAlign(i, j, axCell.GetCurSheet());

                    if ((FontAlign & CVertically) != CVertically) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) + CVertically);
                    if ((FontAlign & Top) == Top) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) - Top);
                    if ((FontAlign & CVertically) == Bottom) axCell.SetCellAlign(i, j, axCell.GetCurSheet(), axCell.GetCellAlign(i, j, axCell.GetCurSheet()) - Bottom);

                }
            }

        }

        private void mnuFormatRowBestHeight()
        {
            int BestHeight = 0, CurSheet = 0;
            CurSheet = axCell.GetCurSheet();
            axCell.GetSelectRange(ref startCol, ref startRow, ref endCol, ref endRow);
            for (int i = startRow; i < endRow; i++)
            {
                BestHeight = axCell.GetRowBestHeight(i);
                if (BestHeight != 0)
                {
                    axCell.SetRowHeight(1, BestHeight, i, CurSheet);
                }
            }
            axCell.Invalidate();
        }

        private void axCell_MenuStart(object sender, AxCELL50Lib._DCell2000Events_MenuStartEvent e)
        {
            if (e.section == 1) //鼠标右键单击在表格区域内
            {
                //axCell.ContextMenuStrip = contextMenuStrip1;
                //PopupMenu mnuInCell '弹出语言本身支持的右键菜单
                axCell.AddPopMenu(1001, "剪切(&T)", 0);
                axCell.AddPopMenu(1002, "复制(&C)", 0);
                axCell.AddPopMenu(1003, "粘贴(&P)", 0);
                axCell.AddPopMenu(1004, "选择性粘贴(&S)...", 0);

                axCell.AddPopMenu(1000, "", 0);
                axCell.AddPopMenu(1005, "输入公式(&I)", 0);
                axCell.AddPopMenu(1006, "格式刷(&M)", 0);
                axCell.AddPopMenu(1000, "", 0);
                axCell.AddPopMenu(1008, "清除内容(&C)     Del", 0);
                axCell.AddPopMenu(1009, "清除公式(&L)", 0);
                axCell.AddPopMenu(1010, "清除全部(&A)", 0);

                axCell.AddPopMenu(1000, "", 0);
                axCell.AddPopMenu(1012, "单元格格式(&C)...", 0);
                //axCell.AddPopMenu(1013, "超级链接(&H)...", 0);


            }
            else if (e.section == 2) //'鼠标右键单击在行标上
            {
                axCell.AddPopMenu(1001, "剪切(&T)", 0);
                axCell.AddPopMenu(1002, "复制(&C)", 0);
                axCell.AddPopMenu(1003, "粘贴(&P)", 0);
                axCell.AddPopMenu(1004, "选择性粘贴(&S)...", 0);
                axCell.AddPopMenu(1000, "", 0);

                axCell.AddPopMenu(1014, "插入表行(&I)", 0);
                axCell.AddPopMenu(1015, "删除表行(&D)", 0);
                axCell.AddPopMenu(1016, "追加表行(&A)", 0);

                axCell.AddPopMenu(1000, "", 0);
                axCell.AddPopMenu(1018, "行高(&H)...", 0);
                axCell.AddPopMenu(1019, "最适合行高(&B)", 0);
                axCell.AddPopMenu(1020, "隐藏(&N)", 0);
                axCell.AddPopMenu(1021, "取消隐藏(&U)", 0);

            }
            else if (e.section == 3) //鼠标右键单击在列标
            {
                axCell.AddPopMenu(1001, "剪切(&T)", 0);
                axCell.AddPopMenu(1002, "复制(&C)", 0);
                axCell.AddPopMenu(1003, "粘贴(&P)", 0);
                axCell.AddPopMenu(1004, "选择性粘贴(&S)...", 0);
                axCell.AddPopMenu(1000, "", 0);

                axCell.AddPopMenu(1022, "插入表列(&I)", 0);
                axCell.AddPopMenu(1023, "删除表列(&D)", 0);
                axCell.AddPopMenu(1024, "追加表列(&A)", 0);
                axCell.AddPopMenu(1000, "", 0);
                axCell.AddPopMenu(1026, "列宽(&W)...", 0);
                axCell.AddPopMenu(1027, "最适合列宽(&W)", 0);
                axCell.AddPopMenu(1028, "隐藏(&N)", 0);
                axCell.AddPopMenu(1029, "取消隐藏(&U)", 0);

            }
            else if (e.section == 5) //鼠标右键单击在页签上
            {
                axCell.AddPopMenu(1040, "插入表页(&I)...", 0);
                axCell.AddPopMenu(1041, "删除表页(&D)...", 0);
                axCell.AddPopMenu(1042, "追加表页(&A)...", 0);
                axCell.AddPopMenu(1000, "", 0);
                axCell.AddPopMenu(1044, "重命名(&R)...", 0);
                axCell.AddPopMenu(1045, "表页排序(&S)...", 0);
                axCell.AddPopMenu(1000, "", 0);
                axCell.AddPopMenu(1047, "表尺寸(&F)...", 0);
                axCell.AddPopMenu(1048, "表页另存为(&O)...", 0);
                if (axCell.IsSheetProtect(axCell.GetCurSheet()) == 1)
                {
                    axCell.AddPopMenu(1049, "表页解锁(&L)...", 0);
                }
                else
                {
                    axCell.AddPopMenu(1049, "表页保护(&L)...", 0);
                }
            }
            else if (e.section == 6) //鼠标右键单击在页签的操作按扭上时
            {
                for (int i = 1; i < axCell.GetTotalSheets(); i++)
                {
                    if ((i - 1) == axCell.GetCurSheet())
                    {
                        axCell.AddPopMenu(104 * 10 + 9 + i, axCell.GetSheetLabel(i - 1), 8);
                    }
                    else
                    {
                        axCell.AddPopMenu(104 * 10 + 9 + i, axCell.GetSheetLabel(i - 1), 0);
                    }
                }
                //TotalMenu = 104 * 10 + 9 + i
            }
            return;
        }

        private void axCell_MenuCommand(object sender, AxCELL50Lib._DCell2000Events_MenuCommandEvent e)
        {
            axCell.GetSelectRange(ref startCol, ref startRow, ref endCol, ref endRow);
            switch (e.itemid)
            {
                case 1001://'剪切
                    axCell.CutRange(startCol, startRow, endCol, endRow);
                    break;
                case 1002://'复制
                    axCell.CopyRange(startCol, startRow, endCol, endRow);
                    break;
                case 1003://'粘贴
                    axCell.Paste(startCol, startRow, 0, 1, 0);
                    break;
                case 1004://选择性粘贴
                    frmPasteSpecial frm = new frmPasteSpecial();
                    frm.CurControl = this;
                    frm.ShowDialog();
                    break;
                case 1005://输入公式
                    axCell.FormulaWizard(axCell.GetCurrentCol(), axCell.GetCurrentRow());
                    break;
                case 1006://格式刷
                    axCell.FormatPainter();
                    break;
                case 1008://清除内容
                    axCell.Clear(1);
                    break;
                case 1009://清除公式
                    axCell.Clear(2);
                    break;
                case 1010://清除全部
                    axCell.Clear(32);
                    break;
                case 1012://单元格格式
                    axCell.CellPropertyDlg();
                    break;
                case 1014://插入行
                    CurrentRow = axCell.GetCurrentRow();
                    axCell.InsertRow(CurrentRow, 1, 0);
                    break;
                case 1015://删除表行
                    axCell.DeleteRow(startRow, endRow - startRow + 1, axCell.GetCurSheet());
                    break;
                case 1016://追加表行
                    axCell.InsertRow(axCell.GetRows(axCell.GetCurSheet()), endRow - startRow + 1, axCell.GetCurSheet());
                    break;
                case 1022://添加表列
                    axCell.InsertCol(startCol, 1, axCell.GetCurSheet());
                    break;
                case 1023://删除列
                    axCell.DeleteCol(startCol, endCol - startCol + 1, axCell.GetCurSheet());
                    break;
                case 1024://追加表列
                    //axCell.DrawLineDlg();
                    axCell.InsertCol(axCell.GetCols(axCell.GetCurSheet()), endCol - startCol + 1, axCell.GetCurSheet());
                    break;
                case 1019://合适行高
                    mnuFormatRowBestHeight();
                    break;
                case 1020: //隐藏行
                    axCell.SetRowHidden(startRow, endRow);
                    break;
                case 1021: //取消隐藏行
                    axCell.SetRowUnhidden(startRow, endRow);
                    break;
                case 1028://隐藏列
                    axCell.SetColHidden(startCol, endCol);
                    break;
                case 1029://取消隐藏列
                    axCell.SetColUnhidden(startCol, endCol);
                    break;
                case 1040: //插入sheet
                    axCell.InsertSheet(axCell.GetCurSheet(), 1);
                    break;
                case 1041://删除sheet
                    axCell.DeleteSheet(axCell.GetCurSheet(), 1);
                    break;
                case 1042://追加sheet
                    //.InsertSheet .GetTotalSheets, 1
                    axCell.InsertSheet(axCell.GetTotalSheets(), 1);
                    break;
                case 1044://重命名sheet
                    frmSetSheetLabel frmLable = new frmSetSheetLabel();
                    frmLable.SheetName = axCell.GetSheetLabel(axCell.GetCurSheet()); //得到原来的
                    frmLable.ShowDialog();
                    if (frmLable.DialogResult == DialogResult.OK)
                    {
                        axCell.SetSheetLabel(axCell.GetCurSheet(), frmLable.SheetName);
                    }
                    break;
                case 1045://表页排序
                    axCell.SortStyleSheetDlg();
                    break;
                case 1047: //表尺寸
                    SetCellSize(); //调用表尺寸的自定义方法
                    break;

                case 1048://Sheet另存为
                    axCell.SaveSheet(axCell.GetCurSheet(), "");
                    break;
                case 1049://表页保护 解锁
                    axCell.WzdChartDlg();
                    break;
                case 8001://自定义-指标化
                    break;
                default:
                    break;
            }
        }

        private bool ShowColorDialog(ref Color color)
        {
            bool selected;
            using (ColorDialog dlg = new ColorDialog())
            {
                dlg.SolidColorOnly = true;
                dlg.AllowFullOpen = false;
                dlg.AnyColor = false;
                dlg.FullOpen = false;
                dlg.CustomColors = null;
                dlg.Color = color;
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    selected = true;
                    color = dlg.Color;
                }
                else
                {
                    selected = false;
                }
            }
            return selected;
        }
        //RGB 转int
        private uint ParseRGB(Color color)
        {
            return (uint)(((uint)color.B << 16) | (ushort)(((ushort)color.G << 8) | color.R));
        }

        private void colorButton_Click(object sender, EventArgs e)
        {
            //字体颜色
            axCell.GetSelectRange(ref startCol, ref startRow, ref endCol, ref endRow);
            int mycolor;
            ColorDialog c = new ColorDialog();
            //c.ShowDialog();//弹出对话框，选中某个颜色。
            if (c.ShowDialog(this) != DialogResult.OK) return; //弹出对话框，选中某个颜色。
            mycolor = (int)ParseRGB(c.Color);

            for (int i = startCol; i <= endCol; i++)
            {
                for (int j = startRow; j <= endRow; j++)
                {
                    if (axCell.FindColorIndex(mycolor, 1) != -1)
                    {
                        axCell.SetCellTextColor(i, j, axCell.GetCurSheet(), axCell.FindColorIndex(mycolor, 1));
                    }

                }
            }
        }

        private void boldButton_Click(object sender, EventArgs e)
        {
            Bold();
        }

        private void italicButton_Click(object sender, EventArgs e)
        {
            Italic();

        }

        private void underlineButton_Click(object sender, EventArgs e)
        {
            Underline();
        }

        private void backColorButton_Click(object sender, EventArgs e)
        {
            //背景颜色
            axCell.GetSelectRange(ref startCol, ref startRow, ref endCol, ref endRow);
            int mycolor;
            ColorDialog c = new ColorDialog();
            //c.ShowDialog();//弹出对话框，选中某个颜色。
            if (c.ShowDialog(this) != DialogResult.OK) return; //弹出对话框，选中某个颜色。
            mycolor = (int)ParseRGB(c.Color);

            for (int i = startCol; i <= endCol; i++)
            {
                for (int j = startRow; j <= endRow; j++)
                {
                    if (axCell.FindColorIndex(mycolor, 1) != -1)
                    {
                        axCell.SetCellBackColor(i, j, axCell.GetCurSheet(), axCell.FindColorIndex(mycolor, 1));
                    }

                }
            }
        }

        private void justifyLeftButton_Click(object sender, EventArgs e)
        {
            JustifyLeft();
        }

        private void justifyCenterButton_Click(object sender, EventArgs e)
        {
            JustifyCenter();
        }

        private void justifyRightButton_Click(object sender, EventArgs e)
        {
            JustifyRight();
        }

        private void justifyFullButton_Click(object sender, EventArgs e)
        {
            JustifyFull();
        }

        private void mergecells_Click(object sender, EventArgs e)
        {
            axCell.GetSelectRange(ref startCol, ref startRow, ref endCol, ref endRow);
            axCell.MergeCells(startCol, startRow, endCol, endRow);
        }

        private void unmergecells_Click(object sender, EventArgs e)
        {
            axCell.GetSelectRange(ref startCol, ref startRow, ref endCol, ref endRow);
            axCell.UnmergeCells(startCol, startRow, endCol, endRow);
        }

        private void CellControl_Load(object sender, EventArgs e)
        {
            //ShowPageBreak
            this.axCell.ShowPageBreak(0); //不显示打印分页线
            SetupFontComboBox();//加载字体
            SetuFontSize();//加载字体大小
            //
            this.axCell.EnableUndo(1); // undo 功能默认禁止 开启的话，比较消耗内存 如果Enable>0允许Undo，否则禁止Undo 
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            //垂直上对齐
            VerticalTop();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            //垂直中对齐
            VerticalCenter();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            //垂直下对齐
            VerticalBottom();
        }

        private void axCell_SelChanged(object sender, AxCELL50Lib._DCell2000Events_SelChangedEvent e)
        {
            //在选择单元格时触发该事件。
            string FontName = axCell.GetFontName(axCell.GetCellFont(e.newcol, e.newrow, axCell.GetCurSheet()));
            fontComboBox.Text = FontName;

            //字体大小
            int FontSize = Convert.ToInt16(axCell.GetCellFontSize(e.newcol, e.newrow, axCell.GetCurSheet()));
            fontSizeComboBox.Text = FontSize.ToString();
            //判断单元格是否为只读属性
            int input = axCell.GetCellInput(e.newcol, e.newrow, axCell.GetCurSheet());
            if (input == 5)
            {
                LockButton.Checked = true;
            }
            else
            {
                LockButton.Checked = false;
            }
            //判断是否可以取消
            GetUndoRedo();
        }
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            //撤销
            GetUndoRedo();
            this.axCell.Undo();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            //重做
            GetUndoRedo();
            axCell.Redo();
        }

        private void GetUndoRedo()
        {
            if (this.axCell.GetUndoState() != 0)
            {
                toolStripUndo.Enabled = true;
            }
            else
            {
                toolStripUndo.Enabled = false;
            }
            //Redo
            if (axCell.GetRedoState() != 0)
            {
                toolstripRedo.Enabled = true;
            }
            else
            {
                toolstripRedo.Enabled = false;
            }

        }

        private void LockButton_Click(object sender, EventArgs e)
        {
            axCell.GetSelectRange(ref startCol, ref startRow, ref endCol, ref endRow);

            int input = axCell.GetCellInput(startCol, startRow, axCell.GetCurSheet());
            if (input != 5)
            {
                for (int i = startCol; i <= endCol; i++)
                {
                    for (int j = startRow; j <= endRow; j++)
                    {
                        axCell.SetCellInput(i, j, axCell.GetCurSheet(), 5); //5只读
                    }
                }
            }
            else
            {
                for (int i = startCol; i <= endCol; i++)
                {
                    for (int j = startRow; j <= endRow; j++)
                    {
                        axCell.SetCellInput(i, j, axCell.GetCurSheet(), 0); //0 不控制
                    }
                }
            }

        }

        private void toolSizeButton_Click(object sender, EventArgs e)
        {
            SetCellSize();
        }

        private void axCell_AllowInputFormula(object sender, AxCELL50Lib._DCell2000Events_AllowInputFormulaEvent e)
        {
            //是否允许激活当前单元格以外的单元格
            //0 不使用系统自带的输入公式对话框 
            //1 使用系统自带的公式输入对话框 
            e.approve = 0;
        }

        private void toolbtnDrawLine_Click(object sender, EventArgs e)
        {
            axCell.DrawLineDlg();
        }



    }
}
